package com.student.xujiang.myallstudent.GsonAndJackson;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.student.xujiang.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JacksonAndGsonMainActivity extends AppCompatActivity {

    @BindView(R.id.bt_gson)
    Button btGson;
    @BindView(R.id.bt_jackson)
    Button btJackson;
    @BindView(R.id.tv_gsonandjackson_result)
    TextView tvGsonandjacksonResult;
    @BindView(R.id.activity_jackson_and_gson_main)
    LinearLayout activityJacksonAndGsonMain;
    @BindView(R.id.tv_gsonandjackson_result2)
    TextView tvGsonandjacksonResult2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jackson_and_gson_main);
        ButterKnife.bind(this);

        //1、注册广播
        EventBus.getDefault().register(JacksonAndGsonMainActivity.this);


    }

    @OnClick({R.id.bt_gson, R.id.bt_jackson})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_gson:
                Test.testGson();
                break;
            case R.id.bt_jackson:
                Test.testJackson();
                break;
        }
    }

    // 5 接收消息 1 参数是消息类 2加注解 表示主线程执行
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEventBus(MessageEvent event) {
        //显示接收的消息
        tvGsonandjacksonResult.setText(event.getJsonString());

        tvGsonandjacksonResult2.setText(event.getSchool().getStudents().get(0).getName());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //2、解注册
        EventBus.getDefault().unregister(JacksonAndGsonMainActivity.this);
    }


}
